Walkthrough 4-3: Transform data
In this walkthrough, you transform and display the flight data into JSON. You will:
· Use the Transform Message component.
· Use the DataWeave visual mapper to change the response to a different JSON structure.
Starting file
If you did not complete the previous walkthrough, you can get a starting file here. This file is also located in the solutions folder of the student files ZIP located in the Course Resources.
Add a Transform Message component
1. Return to Anypoint Studio.
2. In the Mule Palette, select Core and locate the Transform Message component in the Components section.
3. Drag the Transform Message component and drop it after the Select processor.
Review metadata for the transformation input
4. In the Transform Message properties view, look at the input section and review the payload metadata.
Note: If you are using the local Derby database, the properties will be uppercase instead.
Return the payload as JSON
5. In the Transform Message properties view, change the output type from application/java to application/json and change the {} to payload.
Test the application
6. Save the file to redeploy the project.
7. In Advanced REST Client, send the same request; you should get a 200 response and the American flight data represented as JSON.
Note: If you are using the local Derby database, the properties will be uppercase instead.
Review the data structure to be returned by the American flights API
8. Return to your American Flights API in Exchange.
9. Look at the example data returned for the /flights GET method.
10. Notice that the structure of the JSON being returned by the Mule application does not match this example JSON format.
Define metadata for the data structure to be returned by the American flights API
11. Return to Anypoint Studio.
12. In the canvas, click training4-american-wsFlow name.
13. In the training4-american-wsFlow properties view, select the Metadata tab.
14. Click the Add metadata button.
15. In the drop-down menu, select Output: Payload.
16. Click the Edit button.
17. In the Select metadata type dialog box, click the Add button.
18. In the Create new type dialog box, set the type id to american_flights_json.
19. Click Create type.
20. Back in the Select metadata type dialog box, set the first type to JSON.
21. Change the Schema selection to Example.
22. Click the browse button and navigate to the student files.
23. Select american-flights-example.json in the resources/examples folder and click Open; you should see the example data for the metadata type.
24. Click Select.
25. In training4-american-wsFlow, click the Transform Message component; you should now see output metadata in the output section of the Transform Message properties view.
Create the transformation
26. Map fields with the same names by dragging them from the input section and dropping them on the corresponding field in the output section.
· ID to ID
· price to price
· totalSeats to plane > totalSeats
27. Map fields with different names by dragging them from the input section and dropping them on the corresponding field in the output section.
· toAirport to destination
· takeOffDate to departureDate
· fromAirport to origin
· seatsAvailable to emptySeats
· planeType to plane > type
28. Concatenate two fields by dragging them from the input section and dropping them on the same field in the output section.
· code1 to code
· code2 to code
Add sample data (optional)
29. Click the Preview button in the output section.
30. In the preview section, click the Create required sample data to execute preview link.
31. Look at the input section, you should see a new tab called payload with sample data generated from the input metadata.
32. Look at the output section, you should see a sample response for the transformation.
33. In the input section, replace all the ???? with sample values.
34. Look at the output section, you should see the sample values in the transformed data.
Test the application
35. Save the file to redeploy the project.
36. In Advanced REST Client, make another request to http://localhost:8081/flights; you should see all the flight data as JSON again but now with the example JSON format.
Try to retrieve information about a specific flight
37. Add a URI parameter to the URL to make a request to http://localhost:8081/flights/3; you should get a 404 Not Found response with a no listener message.
38. Return to Anypoint Studio.
39. Look at the console; you should get a no listener found for request (GET)/flights/3.